package com.springboot.hotel.service.impl;

import com.anji.captcha.util.AESUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.hotel.model.SysUser;
import com.springboot.hotel.service.SysUserService;
import com.springboot.hotel.mapper.SysUserMapper;
import com.springboot.hotel.xencryption.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.anji.captcha.util.AESUtil.aesDecrypt;

/**
* @author 12641
* @description 针对表【sys_user】的数据库操作Service实现
* @createDate 2023-05-30 02:21:46
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{
    private JavaMailSender javaMailSender;
    private String fromAddress; // 发件人地址
    @Autowired
    public void EmailService(JavaMailSender javaMailSender) {
//        这里需要注意，如果使用的是QQ邮箱，需要开启SMTP服务，开启后会给你一个授权码，这个授权码就是下面的密码
//        为什么这里要用fromAddress = "你的邮箱地址"？
//        因为在配置文件中，我们配置的是一个授权用户，这个授权用户可以发送邮件，但是发件人地址必须是授权用户，否则会报错
        this.javaMailSender = javaMailSender;
        this.fromAddress = "1264161650@qq.com"; // 设置发件人地址为授权用户
    }
    /*
     * to:收件人
     * subject:主题
     * content:内容
     * */
    public void sendEmail(String to,String subject,String content){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(fromAddress);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        javaMailSender.send(message);
    }
    @Override
    public SysUser FindEmail(SysUser sysUser) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("email", sysUser.getEmail());
        SysUser one = getOne(wrapper);
        return one;
    }


    @Override
    public SysUser login(SysUser sysUser) {
        //        密码    AES  解密
        try {
            String aesStr = aesDecrypt(sysUser.getPassword(), sysUser.getKey());
            sysUser.setPassword(aesStr);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        sysUser.setPassword(MD5Util.MD5(sysUser.getUsername(), sysUser.getPassword()));
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", sysUser.getUsername());
        wrapper.eq("password", sysUser.getPassword());
        SysUser one = getOne(wrapper);
        return one;
    }


    @Override
    public int saveSysUser(SysUser sysUser) {
        try {
            String aesStr = aesDecrypt(sysUser.getPassword(), sysUser.getKey());
            sysUser.setPassword(aesStr);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
//        密码加密
        String password = MD5Util.MD5(sysUser.getUsername(), sysUser.getPassword());
        sysUser.setPassword(password);
       return save(sysUser)? 1 : -1;
    }

    @Override
    public PageInfo<SysUser> list(SysUser sysUser, Integer page, Integer limit) {
        // 分页
        PageHelper.startPage(page, limit);
        // 查询条件
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.select("id", "username", "name", "phone", "create_time", "update_time", "state","email","is_del");
        if (sysUser.getUsername() != null && !sysUser.getUsername().equals("")) {
            wrapper.like("username", sysUser.getUsername());
        }
        if (sysUser.getName() != null && !sysUser.getName().equals("")) {
            wrapper.like("name", sysUser.getName());
        }
        if (sysUser.getPhone() != null && !sysUser.getPhone().equals("")) {
            wrapper.like("phone", sysUser.getPhone());
        }
        wrapper.eq("state", 1);
        wrapper.eq("is_del", 0);

        // 查询并封装成 PageInfo 对象
        List<SysUser> sysUsers = getBaseMapper().listSelect(sysUser);
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUsers);
        getBaseMapper().listSelect(sysUser);
        return pageInfo;
    }

    @Override
    public SysUser getUserIdByUsername(String username) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        SysUser one = getOne(wrapper);
        if (one != null){
            return one;
        }
        return null;
    }

    @Override
    public PageInfo<SysUser> selectByIsdel(SysUser sysUser, Integer page, Integer limit) {
        // 分页
        PageHelper.startPage(page, limit);
        // 查询条件
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
//        查询isdel为true的数据
        wrapper.eq("is_del", true);
        // 查询并封装成 PageInfo 对象
        List<SysUser> sysUsers = getBaseMapper().selectList(wrapper);
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUsers);

        return pageInfo;
    }

}




